//
//  DepoistMoneyListView.swift
//  d11
//
//  Created by peale on 2018/10/21.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias BtnMoneyChangeBlock = (_ money: String) -> Void
class DepoistMoneyListView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {
     let  reuseIdentifierID = "DepoistMoneyListCell"
     var arrayList : [String]? //数据列表
     var selectDict : [String:Any]?
    var selectIndex :Int = -1  //选中的下标
    lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: 75, height: 32.5)
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        let collView = UICollectionView.init(frame: CGRect(x:0, y:0, width:kScreenWidth, height:50), collectionViewLayout: layout)
        
        if collView.responds(to:#selector(setter: UIView.layoutMargins)) {
            collView.layoutMargins = .zero;
        }
        collView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth.union(UIView.AutoresizingMask.flexibleHeight)
        collView.register(DepoistMoneyListCell.self, forCellWithReuseIdentifier: "DepoistMoneyListCell")
        collView.showsVerticalScrollIndicator = false
        collView.showsHorizontalScrollIndicator = false
        collView.backgroundColor = UIColor.clear
        collView.delegate = self
        collView.dataSource = self
        return collView
    }()
    var btnMoneyChangeBlock: BtnMoneyChangeBlock?
    func callChangeBlock(_ block: @escaping BtnMoneyChangeBlock) {
        btnMoneyChangeBlock = block
    }
    convenience init(list:[String]) {
        self.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 50))
        self.backgroundColor = UIColor.clear
        self.arrayList  = list
        if self.arrayList == nil {
            self.arrayList = [String]()
        }
        self.selectIndex = -1
        self.addSubview(self.collectionView)
    }
    
    
    override func layoutSubviews() {
        self.collectionView.frame = self.bounds
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayList!.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
//        return 15
//    }
//
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//        return UIEdgeInsetsMake(0, 5, 0, 5)
//    }
//   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
//            return 10
//    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:DepoistMoneyListCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierID, for: indexPath) as! DepoistMoneyListCell
        var isSelect = false
        if self.selectIndex == indexPath.row {
            isSelect = true
        }
        cell.setData(moneyStr: arrayList![indexPath.row],isSelect: isSelect)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        if btnMoneyChangeBlock != nil {
            btnMoneyChangeBlock!(arrayList![indexPath.row])
        }
        self.collectionView.reloadData()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
